#include "b2Render.h"

void DebugDraw::SetOutlet(t_object * outlet) {
	main_outlet = outlet;
}



void DebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) {

	GLCOLOR(color.r, color.b, color.g,1.0f);
	GLBEGIN("line_loop");
	for (int32 i = 0; i < vertexCount; ++i) {
		GLVERTEX(M2P(vertices[i].x), M2P(vertices[i].y));
		
	//	post("x= %f y = %f",M2P(vertices[i].x), M2P(vertices[i].y));
		
	}
	GLEND;
}

void DebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) {

	GLENABLE("blend");
	GLBLENDFUNC("src_alpha", "one_minus_src_alpha");
	GLCOLOR(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f);
	GLBEGIN("tri_fan");
	for (int32 i = 0; i < vertexCount; ++i) {
		GLVERTEX(M2P(vertices[i].x), M2P(vertices[i].y));
	}
	GLEND;
	GLDISABLE("blend")
	

	GLCOLOR(color.r, color.g, color.b, 1.0f);
	GLBEGIN("line_loop");
	for (int32 i = 0; i < vertexCount; ++i)
	{
		GLVERTEX(M2P(vertices[i].x), M2P(vertices[i].y));
	}
	GLEND;
}

void DebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color) {

	//post("DRAW CIRCLE");

	const float32 k_segments = 16.0f;
	const float32 k_increment = 2.0f * b2_pi / k_segments;
	float32 theta = 0.0f;
	
	GLCOLOR(color.r, color.g, color.b, 1.0f);
	GLBEGIN("line_loop");
	for (int32 i = 0; i < k_segments; ++i)
	{
		b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
		GLVERTEX(M2P(v.x), M2P(v.y));
		theta += k_increment;
	}
	GLEND;

}

void DebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color) {

	const float32 k_segments = 16.0f;
	const float32 k_increment = 2.0f * b2_pi / k_segments;
	float32 theta = 0.0f;
	GLENABLE("blend");
	GLBLENDFUNC("src_alpha", "one_minus_src_alpha");
	GLCOLOR(0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f);
	GLBEGIN("tri_fan");
	for (int32 i = 0; i < k_segments; ++i)
	{
		b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
		GLVERTEX(M2P(v.x), M2P(v.y));
		theta += k_increment;
	}
	GLEND;
	GLDISABLE("blend");

	theta = 0.0f;
	GLCOLOR(color.r, color.g, color.b, 1.0f);
	GLBEGIN("line_loop");
	for (int32 i = 0; i < k_segments; ++i)
	{
		b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
		GLVERTEX(M2P(v.x), M2P(v.y));
		theta += k_increment;
	}
	GLEND;

	b2Vec2 p = center + radius * axis;
	GLBEGIN("lines");
	GLVERTEX(M2P(center.x), M2P(center.y));
	GLVERTEX(M2P(p.x), M2P(p.y));
	GLEND;

}

void DebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color) {

	GLCOLOR(color.r, color.g, color.b,1.0);
	GLBEGIN("lines");
	GLVERTEX(M2P(p1.x), M2P(p1.y));
	GLVERTEX(M2P(p2.x), M2P(p2.y));
	GLEND;
	
}

void DebugDraw::DrawXForm(const b2XForm& xf) {

	
	b2Vec2 p1 = xf.position, p2;
	const float32 k_axisScale = 0.4f;
	GLBEGIN("lines");
	
	GLCOLOR(1.0f, 0.0f, 0.0f,1.0f);
	GLVERTEX(M2P(p1.x), M2P(p1.y));
	p2 = p1 + k_axisScale * xf.R.col1;
	GLVERTEX(M2P(p2.x), M2P(p2.y));

	GLCOLOR(0.0f, 1.0f, 0.0f,1.0);
	GLVERTEX(M2P(p1.x), M2P(p1.y));
	p2 = p1 + k_axisScale * xf.R.col2;
	GLVERTEX(M2P(p2.x), M2P(p2.y));

	GLEND;
}

void DebugDraw::DrawPoint(const b2Vec2& p, float32 size, const b2Color& color) {
	GLPOINTSIZE(size);
	GLBEGIN("points");
	GLCOLOR(color.r, color.g, color.b,1.0f);
	GLVERTEX(M2P(p.x), M2P(p.y));
	GLEND;
	GLPOINTSIZE(1.0f);
}


void DebugDraw::DrawString(int x, int y, const char *string, ...) {


	//post("draw string");

	/*
	char buffer[128];

	va_list arg;
	va_start(arg, string);
	vsprintf(buffer, string, arg);
	va_end(arg);

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	int w = glutGet(GLUT_WINDOW_WIDTH);
	int h = glutGet(GLUT_WINDOW_HEIGHT);
	gluOrtho2D(0, w, h, 0);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	glColor3f(0.9f, 0.6f, 0.6f);
	glRasterPos2i(x, y);
	int32 length = (int32)strlen(buffer);
	for (int32 i = 0; i < length; ++i)
	{
		glutBitmapCharacter(GLUT_BITMAP_8_BY_13, buffer[i]);
	}

	glPopMatrix();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	*/
}

void DebugDraw::DrawAABB(b2AABB* aabb, const b2Color& c) {
	post("Draw AABB");
	/*

	GLCOLOR(c.r, c.g, c.b,1.0f);
	GLBEGIN("line_loop");
	GLVERTEX(M2P(aabb->lowerBound.x), M2P(aabb->lowerBound.y));
	GLVERTEX(M2P(aabb->upperBound.x), M2P(aabb->lowerBound.y));
	GLVERTEX(M2P(aabb->upperBound.x), M2P(aabb->upperBound.y));
	GLVERTEX(M2P(aabb->lowerBound.x), M2P(aabb->upperBound.y));
	GLEND;
	*/
}
